<?php


namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Application\Model\Demande;
use Application\Model\Demandes;
use Application\Form\DemandeForm;
use Application\Form\CraForm;
use Application\Form\TraitementdemandeForm;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Application\Model\DemandesTable;
use Zend\Mail;


class MylamisaController extends AbstractActionController
{
	//protected $serviceTable;
	protected $demandesTable;
	
    public function mylamisaAction(){
    	
    	date_default_timezone_set('Europe/Paris');
    	$user_session = new Container('user');
    	$id = $user_session->id_user;
    	$login = $user_session->login;
    	
    	if(isset($user_session) && $user_session->is_connected == 1){
    		
    		$demandeForm = new DemandeForm ();
    		
    		return new ViewModel ( array (
    				
    				'demandeform' => $demandeForm ,
    				
    		) );
    	} else {
    				
	    	 $this->redirect ()->toRoute ( 'connexion', array (
	    		'controller' => 'Intranet',
	    		'action' => 'connexion',
	    	 ) ,
	    	 		array(
	    	 				'query' => array('url' => 'mylamisa'),
	    	 		)
	    	 );
    	}
    }
    
    public function gestionprojetsAction(){
    	$craForm = new CraForm ();	
    	return new ViewModel ( array (
    	
    			'craform' => $craForm ,
    	
    	) );
    	
    }
    public function gestioncongesAction(){
    	
    	date_default_timezone_set('Europe/Paris');
    	$user_session = new Container('user');
    	$id = $user_session->id_user;
    	if(isset($user_session) && $user_session->is_connected == 1){
    		$demande   = $this->getDemandesTable()->getDemandesByUserId($id);
    		
    		//print_r($demande);
    		$demandeForm = new DemandeForm ();
    		$traitementdemandeForm = new TraitementdemandeForm();
    		return new ViewModel ( array (
    				 
    				'demandeform' => $demandeForm ,
    				'traitementdemande'=> $traitementdemandeForm,
    				'reponsetraitement' => $demande,
    				 
    		) );
    	}else {
    				
	    	 $this->redirect ()->toRoute ( 'connexion', array (
	    		'controller' => 'Intranet',
	    		'action' => 'connexion'
	    	 ) );
    	}
	    	
    	 
    }
    
    public function reseauxAction(){
    	$demandeForm = new DemandeForm ();
    	return new ViewModel ( array (
    
    			'demandeform' => $demandeForm ,
    
    	) );
    
    }
    
    public function gestiondeconfAction(){
    	$demandeForm = new DemandeForm ();
    	return new ViewModel ( array (
    
    			'demandeform' => $demandeForm ,
    
    	) );
    
    }
    

  	
    public function getDemandesTable()
    {
    	if (!$this->demandesTable) {
    		$sm = $this->getServiceLocator();
    		$config = $sm->get('config');
    		$config = $config['db'];
    		$dbAdapter = new DbAdapter($config);
    		//return $dbAdapter;
    		$table = new DemandesTable($dbAdapter);
    		//$this->userTable = $sm->get($table);
    		$this->demandesTable = $table;
    	}
    	return $this->demandesTable;
    }
    
    public function getAgendaTable() {
    	if (! $this->agendaTable) {
    		$sm = $this->getServiceLocator ();
    		$config = $sm->get ( 'config' );
    		$config = $config ['db'];
    		$dbAdapter = new DbAdapter ( $config );
    		$table = new AgendaTable ( $dbAdapter );
    		$this->agendaTable = $table;
    	}
    	return $this->agendaTable;
    }
    
    public  function demandeAction(){
    	$demandeForm = new DemandeForm ();
    	$request = $this->getRequest ();
    	date_default_timezone_set('Europe/Paris');
    	$user_session = new Container('user');
    	$nom = $user_session->nom;
    	$prenom  = $user_session->prenom;
    	$from    = $user_session->email;
    		
    	if ($request->isPost ()) {
    
    		$demand = new Demande();
    		$demandeForm->setInputFilter ( $demand->getInputFilter () );
    		$demandeForm->setData ( $request->getPost () );
    
    
    		if ($demandeForm->isValid ()) {
    
    
    			$datas = $demandeForm->getData() ;
    			//$datas->exchangeArray($form->getData());
    
    			$typedemande = $datas['type_demande'];
    			$demande = $datas['demande'];
    
    
    		/*	$mail = new Mail\Message();
    			$mail->setBody($demande)
    			->setFrom('application@lamisabdi.com', $nom)
    			->addTo('lamisa.burundi@gmail.com', $nom)
    			->setSubject($typedemande);
    
    			$transport = new Mail\Transport\Sendmail();
    			$transport->send($mail);
    */
    				
    		//	$fileForm = new InputFileForm ();
    			$result ='Votre demande a été envoyé avec succès';
    				
    			//return $this->redirect ()->toRoute ( 'application/mylamisa/gestionconges' );
    			return new ViewModel(array(
    					//'fileform' =>$fileForm,
    					'demandeform' =>$demandeForm,
    			));
    			
//     			$this->redirect ()->toRoute ( '/Mylamisa/mylamisa', array (
//     					'controller' => 'Mylamisa',
//     					'action' => 'gestionconges'
//     			) );
    
    		}else{
    			//$fileForm = new InputFileForm ();
    			//	$demandeForm = new DemandeForm ();
    			return new ViewModel(array(
    					//'fileform' =>$fileForm,
    					'demandeform' =>$demandeForm,
    			));
    		}
    	}
    
    }
    
    
}
